import { login } from '@/api/user'
import { delToken, getToken, setToken } from '@/utils/storage'
export default {
  namespaced: true,
  state: {
    token: getToken() || ''
  },
  mutations: {
    updateToken (state, val) {
      // 存储到vuex
      state.token = val
      // 存储到本地
      setToken(val)
    },
    logout (state) {
      // 删除state的token
      state.token = ''
      // 删除本地的token
      delToken()
    }
  },
  actions: {
    async loginAction (context, data) {
      const res = await login(data)
      // console.log(res)
      // 将token传给mutations
      context.commit('updateToken', res.data.token)
    }

  }
}
